package com.chb.system.controller.apis;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chb.system.annotation.OperLogAnnotation;
import com.chb.system.service.UserService;
import com.chb.system.vo.UserAndDeptVo;
import com.chb.system.vo.UserVo;
import com.hos.entity.sys.User;
import com.hos.utils.JwtUtils;
import com.hos.utils.WebUtil;
import com.hos.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author: 陈海彬
 * @date: Created in 2023/12/7 20:59
 * @description:
 * @modified By:
 * @version:
 */
@RestController
@RequestMapping("/system/api/user")
@Api(value = "用户管理相关接口", tags = "用户管理相关接口")
public class APIUserController {

    @Resource
    private UserService userService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @PostMapping("/updateUser")
    @ApiOperation(value = "修改用户信息")
    @OperLogAnnotation(business_type = "系统业务-修改用户", operator_type = 1)
    public Result updateUser(@RequestBody User user) {

        return new Result<>(200, "修改成功", userService.updateUser(user));
    }

    @PostMapping("/resetPwd/{ids}/{newPassword}")
    @ApiOperation(value = "修改用户密码")
    @OperLogAnnotation(business_type = "系统业务-修改密码", operator_type = 1)
    public Result<Integer> resetPwd(@PathVariable List<Long> ids, @PathVariable String newPassword) {

        Integer integer = userService.resetPwd(ids, newPassword);
        return new Result<>(200, "修改成功", integer);
    }

    @PostMapping("/getUserById/{id}")
    @ApiOperation(value = "根据Id查询一个用户")
    public Result<User> getUserById(@PathVariable Long id) {
        User user = userService.getUserById(id);
        return new Result<>(200, "查询成功", user);
    }


    // 根据id删用户
    @DeleteMapping("deleteUserByIds/{ids}")
    @ApiOperation(value = "根据用户ID永久删除")
    @OperLogAnnotation(business_type = "系统业务-删除用户", operator_type = 1)
    public Result<Integer> deleteUserByIds(@PathVariable List<Long> ids) {
        Integer integer = userService.deleteUserByIds(ids);
        return new Result<>(200, "删除成功", integer);
    }

    // 添加用户
    @PostMapping("/addUser")
    @ApiOperation(value = "添加用户")
    @OperLogAnnotation(business_type = "系统业务-添加用户", operator_type = 1)
    public Result<Integer> addUser(User user) {
        Integer integer = userService.addUser(user);
        return new Result<>(200, "添加成功", integer);
    }

    @GetMapping("/getUserInfo")
    @ApiOperation(value = "得到用户信息")
    public Result<User> getUserInfo() {
        String token = WebUtil.getRequest().getHeader("token");
        String phone = JwtUtils.getClaimByName(token);
        User user = userService.findUserByPhone(phone);
        return new Result<>(200, "请求成功", user);
    }


    @GetMapping("loginOut")
    @ApiOperation(value = "退出登录信息")
    @OperLogAnnotation(business_type = "系统业务-退出用户", operator_type = 1)
    public Result<Void> loginOit() {
        String token = WebUtil.getToken();
        // 删除左侧角色菜单
        redisTemplate.delete("menu:leftMenu:" + redisTemplate.opsForValue().get("user:") + token);
        // 删除用户信息
        redisTemplate.delete("user:" + token);

        return new Result<>(200, "退出登录", null);
    }

    @PostMapping("/findUserByCond")
    @ApiOperation(value = "条件查询用户信息")
    public Result<Page<User>> findUserByCond(@RequestBody UserVo userVo) {
        return new Result<>(200, "请求成功", userService.findUserByCond(userVo));
    }


    @GetMapping("getAllSchedulingList")
    @ApiOperation(value = "查询所有排班人员信息")
    public Result<List<User>> getAllSchedulingList(Map<String, Object> params) {
        List<User> users = userService.getUserByParams(params);
        return new Result<>(200, "请求成功", users);
    }

    @GetMapping("getUserListByDeptId/{deptId}")
    @ApiOperation("根据科室id查询医生列表信息")
    public Result<List<UserAndDeptVo>> getUserListByDeptId(@PathVariable Long deptId) {
        return new Result<>(200, "查询成功", this.userService.getUserListByDeptId(deptId));
    }


    @GetMapping("getUserListRanges")
    @ApiOperation("随机查询医生列表信息")
    public Result<List<User>> getUserListRanges() {
        return new Result<>(200, "查询成功", this.userService.getUserListRanges());
    }

    @GetMapping("userPhone/{phone}")
    @ApiOperation("判断用户手机号是否重复")
    public Result<String> userPhone(@PathVariable String phone) {
        return this.userService.userPhone(phone);
    }
}